home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 December / PCWDEC07.iso / Software / Freeware / FlashGot 0.6.4 / chrome / flashgot.jar / content / flashgot / flashgotDMOverlay.js < prev    next >
Encoding:
JavaScript  |  2007-08-29  |  6.4 KB  |  234 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.  
  3.     FlashGot - a Firefox extension for external download managers integration
  4.     Copyright (C) 2004-2006 Giorgio Maone - g.maone@informaction.com
  5.  
  6. ***** END LICENSE BLOCK *****/
  7.  
  8. var gFlashGotDMDialog = null;
  9.  
  10. function FlashGotDMDialog() {
  11.   gFlashGotDMDialog = this;
  12.  
  13.   this.url = dialog.mLauncher.source.spec;
  14.   
  15.   try {
  16.       this.referrer = dialog.mContext.QueryInterface(
  17.         Components.interfaces.nsIWebNavigation).currentURI.spec;
  18.   } catch(ex) {
  19.     this.referrer=this.url;
  20.   }
  21.   this.dialog = dialog;
  22.   
  23.  
  24.   this.fname = dialog.mLauncher.suggestedFileName;
  25.   var ext=this.fname.split('.');
  26.   this.ext = ext.length > 0 ? ext[ext.length-1] : "";
  27.   this.extensionExists=gFlashGotService.extensionExists(this.ext);
  28.   const itc = gFlashGotService.interceptor;
  29.   
  30.   if(itc.lastPost && itc.lastPost.URI.spec == this.url && itc.lastPost.isPending()) {
  31.     this.postChannel = itc.lastPost;
  32.   }
  33.   
  34.   if(gFlashGotService.DMS.found && (!itc.bypassAutoStart)
  35.       && (itc.forceAutoStart
  36.           || ( itc.autoStart
  37.             && (itc.interceptAll
  38.                 || this.extensionExists)))) {
  39.     this.download();
  40.     return;
  41.   }
  42.  
  43.   window.setTimeout(function() { gFlashGotDMDialog.init(); }, 0);
  44.   
  45. }
  46.  
  47. FlashGotDMDialog.prototype = {
  48.   get choosen() {
  49.     return gFlashGotService.getPref("dmchoice", false);
  50.   }, 
  51.   set choosen(v) {
  52.     gFlashGotService.setPref("dmchoice", v);
  53.     return v;
  54.   },
  55.   
  56.  
  57.   remember: null,
  58.   choice: null,
  59.   check: null,
  60.  
  61.   forceNormal: function(secondChance) {
  62.     var basicBox = document.getElementById('basicBox');
  63.     var normalBox = document.getElementById('normalBox');
  64.     var self = this;
  65.     if((normalBox && basicBox)) {
  66.       if(normalBox.collapsed && basicBox.collapsed && !secondChance) {
  67.         
  68.         window.setTimeout(function() { self.forceNormal(true); }, 10);
  69.         return;
  70.       }
  71.       if(normalBox.collapsed) {
  72.         basicBox.collapsed = true;
  73.         normalBox.collapsed = false;
  74.         document.getElementById('open').parentNode.collapsed = true;
  75.         var nodes = normalBox.getElementsByTagName('separator');
  76.         for(var j = nodes.length; j-- > 0;) {
  77.           nodes[j].collapsed = true;
  78.         }
  79.       }
  80.     }
  81.     window.setTimeout(self.sizeToContent, 0);
  82.   },
  83.   
  84.   sizeToContent: function() {
  85.     try {
  86.       window.sizeToContent();
  87.     } catch(e) { // bug 367184
  88.       window.resizeTo(window.outerWidth, document.getElementById("container").boxObject.height + 80)
  89.     }
  90.   },
  91.   
  92.   init: function() {
  93.     
  94.     this.forceNormal();
  95.   
  96.     const dmsMenu = this.dmsMenu = document.getElementById("flashgot-dms");
  97.       
  98.     this.remember = document.getElementById("rememberChoice") || document.getElementById("alwaysHandle");
  99.     if(this.remember) {
  100.       this.remember.collapsed = false;
  101.       if(this.remember.id == "rememberChoice" && 
  102.         this.remember.parentNode.previousSibling &&
  103.         this.remember.parentNode.previousSibling.nodeName == "separator") {
  104.         this.remember.parentNode.previousSibling.collapsed = false;
  105.       }
  106.     }
  107.    
  108.     
  109.     this.choice = document.getElementById("flashgot-dmradio");
  110.     this.check = document.getElementById("flashgot-dmcheck");
  111.  
  112.     const dms = gFlashGotService.DMS;
  113.     if(!dms.found) {
  114.       this.choice.setAttribute("disabled", "true");
  115.       if(this.check) this.check.setAttribute("disabled", "true");
  116.       dmsMenu.setAttribute("collapsed", "true");
  117.       return;
  118.     }
  119.     const defaultDM = gFlashGotService.defaultDM; 
  120.     
  121.     
  122.  
  123.     var menuItem;
  124.     var enabledDMSs=0;
  125.     dmsMenu.removeAllItems();
  126.     var dm;
  127.     for(var j=0, len = dms.length; j<len; j++) {
  128.       dm=dms[j];
  129.       if(dm.supported) {
  130.         enabledDMSs++;
  131.         menuItem=dmsMenu.appendItem(dm.name,dm.codeName);
  132.         if(defaultDM==dm.name) {
  133.           dmsMenu.selectedItem=menuItem;
  134.         }
  135.       }
  136.     }
  137.     
  138.     const modeRadioGroup=document.getElementById("mode");
  139.     
  140.     if(enabledDMSs < 2) {
  141.       dmsMenu.setAttribute("collapsed","true");
  142.     } else {
  143.       dmsMenu.addEventListener("popuphidden", function() {
  144.         gFlashGotDMDialog.toggleChoice();
  145.       }, true);
  146.       
  147.       const openRadio = document.getElementById("open");
  148.       if(openRadio) {
  149.         openRadio.width = this.choice.width = Math.max(
  150.           openRadio.boxObject.width, this.choice.boxObject.width
  151.         );
  152.       }
  153.     }
  154.     
  155.     if(this.choosen) {
  156.       if(this.remember) this.remember.checked = this.extensionExists && gFlashGotService.interceptor.autoStart;
  157.       document.getElementById("mode").selectedItem = this.choice;
  158.       if(this.check) this.check.checked = true;
  159.     }
  160.     
  161.     this.toggleChoice();
  162.     modeRadioGroup.addEventListener(
  163.       "select", function(event) {
  164.         gFlashGotDMDialog.toggleChoice(event)
  165.       },true);
  166.     
  167.     var d = document.documentElement;
  168.     d.setAttribute('ondialogaccept',
  169.       'if(gFlashGotDMDialog.dialogAccepted()) { '
  170.       + document.documentElement.getAttribute('ondialogaccept')
  171.       +'}');
  172.       d.setAttribute("onblur", "if(dialog) {" + d.getAttribute("onblur") + " }");
  173.   }
  174. ,
  175.   toggleChoice: function() {
  176.     var dmchoice = document.getElementById("mode").selectedItem == this.choice;
  177.     
  178.     this.choosen = dmchoice;
  179.     var remember = this.remember;
  180.     
  181.     if(dmchoice) {
  182.       this.dmsMenu.removeAttribute("disabled");
  183.       window.setTimeout(
  184.         function() { 
  185.           document.documentElement.getButton('accept').disabled = false;
  186.         }, 10);
  187.       if(remember) {
  188.         remember.disabled = false;
  189.       }
  190.     } else {
  191.       this.dmsMenu.setAttribute("disabled", true);
  192.     }
  193.   }
  194. ,
  195.    dialogAccepted: function() {
  196.     if(this.choosen) {
  197.       if(this.remember && this.remember.checked) {
  198.         gFlashGotService.addExtension(this.ext);
  199.         gFlashGotService.interceptor.autoStart = true;
  200.       }
  201.       if(this.dmsMenu.selectedItem) {
  202.         gFlashGotService.defaultDM = this.dmsMenu.selectedItem.getAttribute("label");
  203.       }
  204.       this.download();
  205.       return false;
  206.     } else {
  207.       return true;
  208.     }
  209.   }
  210. ,
  211.   download: function() {
  212.     var links=[ {
  213.        href: this.url, 
  214.        description: this.fname
  215.     } ];
  216.     links.referrer = this.referrer;
  217.     if(this.postChannel) {
  218.       links.postData = gFlashGotService.interceptor.extractPostData(this.postChannel);
  219.     }
  220.     gFlashGotService.download(links);
  221.     with(document.documentElement) {
  222.       removeAttribute('ondialogaccept');
  223.       removeAttribute('onblur');
  224.       removeAttribute('onfocus');
  225.       cancelDialog();
  226.     }
  227.   }
  228. }
  229.  
  230. window.addEventListener("load",  function(e) { new FlashGotDMDialog(); }, false);
  231.  
  232.  
  233.  
  234.